package com.yt.user.api.cms;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.common.utils.StrUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Arrays;
import java.util.List;
import com.yt.user.models.service.ICustomerInfoService;
import com.yt.user.models.bo.CustomerInfoSaveBO;
import com.yt.user.models.bo.CustomerInfoBO;
import com.yt.user.models.dto.CustomerInfoDTO;

/**
 * 客户信息控制器
 *
 * @author yzz
 * @Date 2025-09-03 14:13:56
 */

@RestController
@RequestMapping("/cms/customerInfo")
@Slf4j
@Api(value = "CustomerInfoController",tags = "客户信息接口")
public class CustomerInfoController {

    @DubboReference
    private ICustomerInfoService customerInfoService;

    @ApiOperation(value = "添加客户信息",notes = "添加客户信息")
    @PostMapping(value = "/insert")
    public Result insert(CustomerInfoSaveBO saveBO) {
        boolean flag = customerInfoService.add(saveBO);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100007);
        }
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存客户信息",notes = "保存客户信息")
    public Result save(CustomerInfoSaveBO saveBO){
        if(saveBO.getId() == null){
            //添加
            return  insert(saveBO);
        } else {
            return  update(saveBO);
        }
    }

    @ApiOperation(value = "修改客户信息",notes = "修改客户信息")
    @PostMapping("/update")
    public Result update(CustomerInfoSaveBO saveBO){
        CustomerInfoDTO dto = new CustomerInfoDTO();
        BeanUtils.copyProperties(saveBO, dto);
        dto.setId(saveBO.getId());
        boolean flag = customerInfoService.updateById(dto);
        if (flag){
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100008);
        }
    }

    @PostMapping("/updateStatus")
    @ApiOperation(value = "修改状态",notes = "修改状态")
     @ApiImplicitParams({
             @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
             ,@ApiImplicitParam(name = "status",value = " 状态（0：禁用，1：启用）",required = true,paramType = "query", dataType = "Integer")
     })
     public Result updateStatus(Long id, Integer status){
         CustomerInfoDTO dto = new CustomerInfoDTO();
         dto.setId(id);
         dto.setStatus(status);
         dto.setUpdateTime(new Date()); // 更新时间
        boolean flag = customerInfoService.updateById(dto);
         if(flag){
             return new Result<>();
         } else {
             return new Result<>(CodeEnum.FAIL_100008);
         }
     }

    @ApiOperation(value = "逻辑删除客户信息", notes = "逻辑删除客户信息")
    @ApiImplicitParam(name = "id", value = "客户ID", required = true, paramType = "query", dataType = "Long")
    @PostMapping("/delete")
    public Result delete(@RequestParam("id") Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        
        CustomerInfoDTO dto = customerInfoService.selectById(id);
        if (dto == null) {
            return new Result<>(CodeEnum.FAIL_100003);
        }
        
        // 设置逻辑删除标识为已删除(1)
        dto.setIsDel(1);
        dto.setUpdateTime(new Date()); // 更新时间
        boolean flag = customerInfoService.updateById(dto);
        if (flag) {
            return new Result<>();
        } else {
            return new Result<>(CodeEnum.FAIL_100009);
        }
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除客户信息",notes = "批量删除客户信息")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result deleteBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        customerInfoService.deleteBatchIds(Arrays.asList(arr));
        return new Result<>();
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<CustomerInfoDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        CustomerInfoDTO dto = customerInfoService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "客户信息列表",notes = "客户信息列表")
    @PostMapping("/queryPageList")
    public Result<Page<CustomerInfoDTO>> queryPageList(CustomerInfoBO bo){
        Wrapper<CustomerInfoDTO> wrapper = new EntityWrapper<>();

                if (bo.getStatus() != null){
                    wrapper.eq("status", bo.getStatus());
                }

        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<CustomerInfoDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = customerInfoService.selectPage(page, wrapper);

        return new Result<>(page);
    }
}
